<?php

class FrontController{
	private $params;
	private static $instance;
	private $view;
	
	private function __construct(){
		var_dump($_REQUEST);
		if(isset($_GET)){
			
			if(isset($_GET['controller']) && !Validation::String($_GET['controller'])){
				$this->params['controller'] = $_GET['controller'];
			}
			if(isset($_GET['action']) && !Validation::String($_GET['action'])){
				$this->params['action'] = $_GET['action'];
			}	
			
		}
		if(isset($_POST)){
			if(isset($_POST['email']) && Validation::Email($_POST['email'])){
				$this->params['email'] = $_POST['email'];
			}
			if(isset($_POST['password']) && !Validation::String($_POST['password'])){
				$this->params['password'] = $_POST['password'];
			}
			
		}
		var_dump($this->params);
		//TODO:править параметры
		$this->view = new View();
	}
	
	public static function instance(){
		if (NULL == FrontController::$instance){
			FrontController::$instance = new FrontController();
		}
		return FrontController::$instance;
	}
	
	
	public function run(){
		$size = sizeof($this->params);
		if($size<1 || empty($this->params['controller'])){
			$actionController = ActionFabricController::getController('Index', $this->view);
		} else {
			switch ($this->params['controller']){
				case 'user':
					//TODO: НЕ  РЕАЛИЗОВАНН AddActionController
					$actionController = ActionFabricController::getController('user', $this->view, $this->params);
					break;
				
				default:
					$actionController = ActionFabricController::getController('Index', $this->view);
					
			}
		}
		
		$actionController->run();
		//TODO:НАЛИЧЕЕ $actionController
		$this->view->render();
		echo $this->view->getErrorString();
	}
	
}
?>